import { ArticleService } from '../../service/article.service';
import { AbstractController, Group, Restful, Autowire } from 'ts-express-restful';
import { Request, Response, NextFunction } from 'express-serve-static-core';

@Group("admin")
@Restful()
export class ArticleController extends AbstractController { 

    @Autowire
    public articleService!: ArticleService;
    $isValidId(id: string) { 
        return /^\d+$/.test(id);
    }
    async get(req: Request, res: Response, next: NextFunction) { 
        let { id } = req.params;
        let artcile = await this.articleService.get(id);
        res.json(artcile);
    }

    async add(req: Request, res: Response, next: NextFunction) { 
        let article = req.body;
        article = await this.articleService.save(article);
        res.json(article);
    }
    async find(req: Request, res: Response, next: NextFunction) { 
        let { page, pagesize } = req.query;
        let pager = await this.articleService.find({}, page, pagesize);
        res.json(pager);
    }
}